package se.gtg.einstein;

import java.util.ArrayList;

public class SearchList {
	private ArrayList<SearchRecord> list = new ArrayList<SearchRecord>();
	private String searchWord;
	
	public SearchList(String searchWord){
		this.searchWord = searchWord;
	}
	
	public void add(String name, String price){
		list.add(new SearchRecord(name, price));
	}
	
	public void add(SearchRecord searchRecord){
		list.add(searchRecord);
	}
	
	public boolean hasDoublets() {
		return list.size() > 1;
	}
	
	public String getSearchWord(){
		return searchWord;
	}
	
	public ArrayList<SearchRecord> getSearchListArray(){
		return list;
	}
	
	public SearchRecord getSearchRecordByPresentation(String presentation) {
		for (SearchRecord searchRecord : list) {
			if (presentation.startsWith(searchRecord.name)) return searchRecord;
		}
		
		return null;
	}
	
	public class SearchRecord {
		private String presentation;
		private String name;
		private double price;
		
		public SearchRecord(String name, String price) {
			presentation = name + ", " + price + "kr";
			this.name = name;
			try {
				this.price = Double.parseDouble(price);
			} catch (NumberFormatException e) {
				this.price = 13.37;
			}
		}
		
		public String getPresentation() {
			return presentation;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public double getPrice() {
			return price;
		}
		public void setPrice(double price) {
			this.price = price;
		}
	}
}
